package com.wangbaokang.duanxin;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TabHost;

/**
 * Created by wangbaokang on 2016-08-18.
 */
public class MainActivity extends TabActivity implements View.OnClickListener {

    private TabHost tabHost;
    private LinearLayout llConversation;
    private LinearLayout llFolider;
    private LinearLayout llGroup;
    private ImageView slideBackground;
    private int itemLength;//背景图片的移动距离，即屏幕宽度的1/3
    private int lastPosition;//滑动背景的上一次位置
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        tabHost = getTabHost();
        addTab("conversation", "会话", R.drawable.tab_conversation, ConversationUI.class);
        addTab("folder", "文件夹", R.drawable.tab_folder, FolderUI.class);
        addTab("group", "群组", R.drawable.tab_group, GroupUI.class);

        llConversation = (LinearLayout) findViewById(R.id.ll_conversation);
        llFolider = (LinearLayout) findViewById(R.id.ll_folder);
        llGroup = (LinearLayout) findViewById(R.id.ll_group);
        slideBackground = (ImageView) findViewById(R.id.iv_slide_background);

        llConversation.setOnClickListener(this);
        llFolider.setOnClickListener(this);
        llGroup.setOnClickListener(this);


        //获得view树的全局观察者 并添加这个全局的layout监听
        llConversation.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                //由于此方法会执行多次 而我们只要执行一次就行了 所以执行一次后讲全局的layout监听取消 此时this指的是内部的匿名对象
                llConversation.getViewTreeObserver().removeOnGlobalLayoutListener(this);

                int width = llConversation.getWidth();
                int height = llConversation.getHeight();
                int left = llConversation.getLeft();
                int top = llConversation.getTop();

                RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) slideBackground.getLayoutParams();
                layoutParams.width = width;
                layoutParams.height = height;
                layoutParams.leftMargin = left;
                layoutParams.topMargin = top;

                //讲llconversation父控件的宽度给itemLength
                itemLength = ((ViewGroup)llConversation.getParent()).getWidth();

            }
        });

    }

    /**
     * 给tabHost添加标签
     * @param tag     标签的命名
     * @param label   标签显示的文字
     * @param iconId  标签显示的图标
     * @param clazz   该标签对应的显示内容
     */
    private void addTab(String tag, CharSequence label, int iconId, Class<?> clazz){
        //创建新的标签 tag 是该标签的命名  此命名是tabHost用来管理标签的标示
        TabHost.TabSpec tabSpec = tabHost.newTabSpec(tag);
        //给标签添加文字和图标
        tabSpec.setIndicator(label, getResources().getDrawable(iconId));
        //给标签添加对应的显示内容
        Intent intent = new Intent(this, clazz);
        tabSpec.setContent(intent);

        tabHost .addTab(tabSpec);

    }

    /**
     * 相应标签的相应事件
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ll_conversation://会话标签
                //判断页面是否是会话页面 如果不是 切换至会话页面
                if (!"conversation".equals(tabHost.getCurrentTabTag())){
                    tabHost.setCurrentTabByTag("conversation");
                    slideBackground.startAnimation(getAnim(0));
                    lastPosition = 0;
                }
                break;

            case R.id.ll_folder://文件夹标签
                //判断页面是否是文件夹页面 如果不是 切换至文件夹页面
                if (!"folder".equals(tabHost.getCurrentTabTag())){
                    tabHost.setCurrentTabByTag("folder");
                    slideBackground.startAnimation(getAnim(itemLength));
                    lastPosition = itemLength;
                }
                break;

            case R.id.ll_group://群组标签
                //判断页面是否是群组页面 如果不是 切换至群组页面
                if (!"group".equals(tabHost.getCurrentTabTag())){
                    tabHost.setCurrentTabByTag("group");
                    slideBackground.startAnimation(getAnim(itemLength * 2));
                    lastPosition = itemLength * 2;
                }
                break;

        }
    }

    private TranslateAnimation getAnim(int destPosition){

        TranslateAnimation anim = new TranslateAnimation(lastPosition, destPosition, 0, 0);
        anim.setDuration(500);
        anim.setFillAfter(true);

        return anim;
    }
}
